﻿using System;
using UnityEditor;
using UnityEngine;

namespace SimpleEditor.ExcelTool
{
    public class ConfigDataSettingWindow : EditorWindow 
    {
        [MenuItem("Simple Tools/Config Data/Setting")]
        static void OpenWindow()
        {
            var window = GetWindow<ConfigDataSettingWindow>();
            window.name = "Config Setting";
        }

        private void OnEnable()
        {
            if (_configAssetData == null)
            {
                _configAssetData = AssetDatabase.LoadAssetAtPath<ExcelToolConfigAssetData>(ConfigAssetPath);
            }

            if (_serializedObject == null)
            {
                _serializedObject = new SerializedObject(_configAssetData);
            }

            _excelPathProperty = _serializedObject.FindProperty("ExcelPath");
            _codePathProperty = _serializedObject.FindProperty("CodePath");
            _dataPathProperty = _serializedObject.FindProperty("DataPath");

            _guiStyle = EditorStyles.miniButtonLeft;
            _guiStyle.fixedWidth = 80;
        }

        private void OnGUI()
        {
            EditorGUILayout.LabelField("Setting");
            EditorGUILayout.Space();

            EditorGUI.DrawSelectFolder(_excelPathProperty, _guiStyle);
            EditorGUI.DrawSelectFolder(_codePathProperty, _guiStyle);
            EditorGUI.DrawSelectFolder(_dataPathProperty, _guiStyle);

            if (GUILayout.Button("Save"))
            {
                _serializedObject.ApplyModifiedProperties();
                Debug.Log("Save Success !!!");
            }
        }

        private ExcelToolConfigAssetData _configAssetData;
        private const string ConfigAssetPath = "Assets/SimpleEditor/Editor/ExcelTool/ExcelToolConfigData.asset";
        private SerializedObject _serializedObject;
        private SerializedProperty _excelPathProperty;
        private SerializedProperty _codePathProperty;
        private SerializedProperty _dataPathProperty;
        private GUIContent _guiContent;
        private GUIStyle _guiStyle;
    }
}
